package com.example.r2022913.service.WxService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.r2022913.common.Constant;
import com.example.r2022913.exception.ExceptionEnum;
import com.example.r2022913.exception.SelfException;
import com.example.r2022913.model.dao.WxDao.SwiperDao;
import com.example.r2022913.model.dao.WxDao.WcDao;
import com.example.r2022913.model.entity.Administration.UserEntity;
import com.example.r2022913.model.entity.WxEntity.SwiperEntity;
import com.example.r2022913.model.entity.WxEntity.WcEntity;
import com.example.r2022913.service.WxService.SwiperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SwiperServiceImpl extends ServiceImpl<SwiperDao, SwiperEntity> implements SwiperService {


    @Override
    public IPage swiperList(Integer pageNum, Integer pageSize, Map<String, Object> params, HttpServletRequest httpServletRequest) throws URISyntaxException {
        //创建一个QueryWrapper用于查询
        QueryWrapper<SwiperEntity> queryWrapper = new QueryWrapper<>();


        //从常量中获取ip
        String ip = Constant.IP;

        //得到全部记录 存到page中
        IPage<SwiperEntity> page = this.page(new Page<SwiperEntity>(pageNum, pageSize), queryWrapper);

        System.out.println(page.getRecords());

        //查看requesturl
//        URI uri = new URI(httpServletRequest.getRequestURL()+"");
//        System.out.println("uri是"+ uri);
//        log.info("得到request-url是==>{}",uri);

        //查看处理后的host主机
//        URI host = getHost(new URI(httpServletRequest.getRequestURL() + ""));
//        log.info("处理后的host主机是==>{}",host);
//        System.out.println("host是"+ host);

        //把host和后盾的imageuri结合 处理后再返回给前端

        //对page中的数据进行进一步处理
        //1 得到page中的数据records
        List<SwiperEntity> records = page.getRecords();

        //2 使用流处理 对每一个SwiperEntity对象 的swimage进行处理 再返回给前端 使前端能够访问
        List<SwiperEntity> collect = records.stream().map((item) -> {
            // 拼接
            String finalImageUrl = ip + item.getSwImage();

            //重新赋值
            //将数据库的 /profile/upload/2022/09/29/0d40c24b264aa511_20220929170126A001.jpg
            //变为 http://localhost:8082/profile/upload/2022/09/29/0d40c24b264aa511_20220929170126A001.jpg
            item.setSwImage(finalImageUrl);

            return item;
        }).collect(Collectors.toList());

        page.setRecords(collect);

        return page;
    }

    //自定义URI
    private URI getHost(URI uri) {
        URI effectiveURI;
        try {
            effectiveURI = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(),
                    null, null, null);
        } catch (URISyntaxException e) {
            effectiveURI = null;
        }
        return effectiveURI;
    }
}
